package com.tnl.app.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.tnl.app.entity.Evaluate;
import com.tnl.base.BaseDAO;
import com.tnl.utils.DateUtil;


@Repository
public class EvaluateDAO {
	private static final String SQL_GET_ID = "select * from tb_evaluate  where id = ?";
	private static final String SQL_GET_HUNTER_EVALUATE = "select * from tb_evaluate  where hunterId = ? order by buyerEvalTime desc";
	private static final String SQL_GET_ORDERID = "select * from tb_evaluate  where orderId=?";
	private static final String SQL_GET_BUYEREVA = "select * from tb_evaluate  where buyerId=? order by id desc";
	private static final String SQL_GET_TYPE_CONTENT = "SELECT * from tb_evaluate where 1=1 limit ?,?";
	private static final String SQL_GET_ALL = "select * from tb_evaluate ";
	private static final String SQL_SAVE = "INSERT INTO tb_evaluate (orderId, buyerId, buyerName, hunterId, hunterName, orderdescIds, buyerEvalScore, buyerEvalTime, buyerEvalContent, buyerEvalStatus, status, anonymous) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)";
	
	public List<Evaluate> getALL() {

		return BaseDAO.queryForList(Evaluate.class, SQL_GET_ALL);

	}
	
	public Evaluate getById(int Id) {
		return  BaseDAO.queryForObject(Evaluate.class, SQL_GET_ID, Id);
	}
	
	public Evaluate getByOrderId(Long Id) {
		return  BaseDAO.queryForObject(Evaluate.class, SQL_GET_ORDERID, Id);
	}
	
	public List<Evaluate> getByContent(int offset,Integer pageSize){
		return  BaseDAO.queryForList(Evaluate.class, SQL_GET_TYPE_CONTENT,offset,pageSize);
	}
	
	public List<Evaluate> getHunterByEva(Integer hunterid){
		return BaseDAO.queryForList(Evaluate.class, SQL_GET_HUNTER_EVALUATE, hunterid);
	}
	public int save(Evaluate eva) {
		if (eva == null) {
			return -1;
		}
		Long id = BaseDAO.save(SQL_SAVE, eva.getOrderId(), eva.getBuyerId(),
				eva.getBuyerName(), eva.getHunterId(), eva.getHunterName(),
				eva.getOrderdescIds(), eva.getBuyerEvalScore(), eva.getBuyerEvalTime(),
				eva.getBuyerEvalContent(), eva.getBuyerEvalStatus(), eva.getStatus(),
				eva.getAnonymous());
		if (id != null) {
			return id.intValue();
		}
		return -1;
	}
	
	private static final String UPDATE_EVALUATE="update tb_evaluate set hunterReplyTime=?,hunterReplyContent=? where evalId =?";
	public int updateEvaluate(String replyContent,Integer evaid){
		return BaseDAO.execute(UPDATE_EVALUATE, DateUtil.currentTime(),replyContent,evaid);
	}
	
	
	public List<Evaluate> getBuyerEvaId(String openid){
		return BaseDAO.queryForList(Evaluate.class, SQL_GET_BUYEREVA, openid);
	}
	
	
}
